package com.googlecode.javatestcomparison.dao;

import static org.junit.Assert.assertEquals;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.googlecode.javatestcomparison.Item;
import com.googlecode.javatestcomparison.dao.SearchDao;


public class SearchDaoIntegrationTest
{
   @BeforeClass
   public static void setupDatabase() throws Exception
   {
      Class.forName("org.sqlite.JDBC");
      Connection conn = DriverManager.getConnection("jdbc:sqlite:bin/mydatabase");
      
      PreparedStatement statement;
      
      statement = conn.prepareStatement("CREATE TABLE item ( name VARCHAR)");
      statement.execute();
      
      statement = conn.prepareStatement("INSERT INTO item (name) VALUES ('daniel')");
      statement.execute();
      
      statement = conn.prepareStatement("INSERT INTO item (name) VALUES ('jeni')");
      statement.execute();
      
      conn.close();      
   }
   
   @AfterClass
   public static void destoryDatabase() throws Exception
   {
      Class.forName("org.sqlite.JDBC");
      Connection conn = DriverManager.getConnection("jdbc:sqlite:bin/mydatabase");
      
      PreparedStatement statement;
      
      statement = conn.prepareStatement("DROP TABLE item");
      statement.execute();
      
      conn.close();   
   }
   
   @Test
   public void shouldReturnTwoRecords() throws Exception
   {
      SearchDao dao = SearchDao.getInstance();
      List<Item> items = dao.searchForItems("");
      
      assertEquals(2, items.size());
      assertEquals("daniel", items.get(0).getName());
      assertEquals("jeni", items.get(1).getName());
   }
}
